<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 应用配置
 */
namespace app\ais\controller;
use app\ais\model\AisConfig;

class Setting extends Common{

    /**
     *  应用配置
     * @return void
     */
    public function config(){
        if(request()->isAjax()){
            $data = [
                'service_telephone'  => $this->request->param('service_telephone/s'),
                'city_only_price'    => $this->request->param('city_only_price/f', 0),
                'city_sharing_price' => $this->request->param('city_sharing_price/f', 0),
                'coupon_is_pay_open' => $this->request->param('coupon_is_pay_open/d', 0),
                'coupon_longtime'    => $this->request->param('coupon_longtime/d', 0),
                'group_join'         => $this->request->param('group_join/d', 0),
                'group_member'       => $this->request->param('group_member/d', 0),
                'group_profit'       => $this->request->param('group_profit/d', 0),
                'mchid'              => $this->request->param('mchid/d',0),
                'charge'             => $this->request->param('charge/d',0),
            ];
            $validate = $this->validate($data,'Config.save');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  AisConfig::configs($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'data' => ['url' => url('setting/config')],'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['pathMaps'] = [['name' =>' 系统配置','url' => url("setting/config")]];
            $view['tabs']     = [
                ['name' =>'系统配置','url' =>url('setting/config'),'action' => 1],
                ['name' =>'服务协议','url' =>url('setting/sla')],
                ['name' =>'应用接口','url' =>url('setting/api')],
                ['name' =>'应用文案','url' =>url('setting/writer')],
            ];
            $view['info']     = AisConfig::getConfig($this->member_miniapp_id);
            return view()->assign($view);

        }
    }

    //文案配置
    public function writer(){
        $info =  AisConfig::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
        if(request()->isAjax()){
            $data = [
                'shore_img'  => $this->request->param('shore_img/s'),
                'shore_text' => $this->request->param('shore_text/s'),
                'mch_text'   => $this->request->param('mch_text/s'),
                'mch_qrcode' => $this->request->param('mch_qrcode/s'),
            ];
            $validate = $this->validate($data,'Config.writer');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $config['writer'] = json_encode($data);
            $result = AisConfig::configs($config,$this->member_miniapp_id);
            if($result){
                return enjson(200,'操作成功',['url' => url('setting/writer')]);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
                return enjson(0,'操作失败');
            }
        }else{
            $view['pathMaps'] = [['name' =>' 文案配置','url' => url("setting/writer")]];
            $view['tabs']     = [
                ['name' =>'系统配置','url' =>url('setting/config')],
                ['name' =>'服务协议','url' =>url('setting/sla')],
                ['name' =>'应用接口','url' =>url('setting/api')],
                ['name' =>'应用文案','url' =>url('setting/writer'),'action' => 1],
            ];
            $view['article'] = empty($info->writer) ? '' : json_decode($info->writer,true);
            return view()->assign($view);
        }
    }
    /**
     * 模板消息配置
     */
    public function api(){
        if(request()->isAjax()){
            $data = [
                'qq_lbs_key'    => $this->request->param('qq_lbs_key/s'),
            ];
            $result =  AisConfig::configs($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'data' => ['url' => url('setting/api')],'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['config'] = AisConfig::getConfig($this->member_miniapp_id);
            $view['pathMaps'] = [['name' =>' 模板消息','url' => url("setting/api")]];
            $view['tabs']     = [
                ['name' =>'系统配置','url' =>url('setting/config')],
                ['name' =>'服务协议','url' =>url('setting/sla')],
                ['name' =>'应用接口','url' =>url('setting/api'),'action' => 1],
                ['name' =>'应用文案','url' =>url('setting/writer')],
            ];
            return view()->assign($view);
        }
    }

    /**
     * 增加会员制度
     * @return void
     */
    public function sla(){
        if(request()->isAjax()){
            $data['sla'] = $this->request->param('sla/s');
            $result =  AisConfig::configs($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'url' => url('setting/sla'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['pathMaps'] = [['name' =>' 服务协议','url' => url("setting/sla")]];
            $view['tabs']     = [
                ['name' =>'系统配置','url' =>url('setting/config')],
                ['name' =>'服务协议','url' =>url('setting/sla'),'action' => 1],
                ['name' =>'应用接口','url' =>url('setting/api')],
                ['name' =>'应用文案','url' =>url('setting/writer')]
            ];
            $view['config']  = AisConfig::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
            return view()->assign($view);
        }
    }

    /**
     *  同城信息配置
     * @return void
     */
    public function info(){
        if(request()->isAjax()){
            $param['info'] = $this->request->param('info/a');
            $validate = $this->validate($param,'Config.info');
            if(true !== $validate){
                return json(['code'=>0,'msg'=> $validate]);
            }
            $result =  AisConfig::info($param,$this->member_miniapp_id);
            if($result){
                return enjson(200,['url' => url('setting/info')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['pathMaps'] = [['name' =>'推广设置','url' => url("setting/info")]];
            $view['tabs']     = [
                ['name' =>'主题管理','url' =>url('info.cate/index')],
                ['name' =>'模板库','url' =>url('info.tpl/index')],
                ['name' =>'推广设置','url' =>url('setting/info'),'action' => 1],
            ];
            $config  = AisConfig::getConfig($this->member_miniapp_id);
            $view['info'] = empty($config->info) ? [] : json_decode($config->info,true);
            return view()->assign($view);
        }
    }
}